package tq.com.tqteacher.adapt;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;

import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqcom.recyclerAdapter.BaseSectionAdapter;
import tq.com.tqcom.recyclerAdapter.declear.IRecyclerItem;
import tq.com.tqcom.recyclerAdapter.declear.IRecyclerSection;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqteacher.R;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.model.TQNewPublish;
import tq.com.tqteacher.model.TQPublishItem;
import tq.com.tqteacher.model.TQPublishSection;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.optimize.view.TQViewOptimizeCardDetailInfo;
import tq.com.tqteacher.view.TQViewPublishDetailInfo;

public class TQHomePublishAdapter extends BaseSectionAdapter<RecyclerView.ViewHolder> {

    private Context context;
    private int type = 0;

    public TQHomePublishAdapter(Context c){
        context = c;
    }
    public TQHomePublishAdapter(Context c, int type){
        context = c;
        this.type = type;
    }
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        TQPublishItem item = (TQPublishItem) getItemWithPos(i);
        if(item.getType() == IRecyclerItem.TYPE_FOOTER && viewHolder instanceof TQFootViewHold){
            //bind data
            ((TQFootViewHold)viewHolder).bindSection((TQPublishSection) getItemSectionWithPos(i));
        }else if(item.getType() == IRecyclerItem.TYPE_NOMRAL && viewHolder instanceof TQItemViewHold){
            ((TQItemViewHold)viewHolder).bindData(item);
        } else if(item.getType() == IRecyclerItem.TYPE_HEADER && viewHolder instanceof  TQHeaderViewHold){
            ((TQHeaderViewHold)viewHolder).bindData(item);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if(i == TQPublishItem.TYPE_HEADER){
            View v = LayoutInflater.from(context).inflate(R.layout.vh_publish_header_layout, viewGroup, false);
            return new TQHeaderViewHold(v);
        }else if(TQPublishItem.TYPE_FOOTER == i){
            View v = LayoutInflater.from(context).inflate(R.layout.vh_publish_footer_layout, viewGroup, false);
            return new TQFootViewHold(v);
        }else{
            View v = LayoutInflater.from(context).inflate(R.layout.vh_publish_item_layout, viewGroup, false);
            return new TQItemViewHold(v);
        }
    }

    public void update(TQPublishItem pPublish){
        int pos = getItemPos(pPublish);
        if(pos != -1){
            notifyItemChanged(pos);
        }

    }


    public class TQItemViewHold extends RecyclerView.ViewHolder{
        protected TQViewOptimizeCardDetailInfo mTQViewOptimizeCardDetailInfo;
        protected TQViewPublishDetailInfo mViewPublishDetailInfo ;
        protected ImageView typeView;
        private TQPublishItem mPublishItem;

        private TextView subjectView;
        private ImageView userHeaderView;
        private TextView userName;
        private ImageView publishStatus;

        public TQItemViewHold(View v){
            super(v);
            FrameLayout content = v.findViewById(R.id.content0);
            if(type == 1){
                mTQViewOptimizeCardDetailInfo = new TQViewOptimizeCardDetailInfo(context, null);
                mTQViewOptimizeCardDetailInfo.setIClickListener(mIClickListener);
                content.addView(mTQViewOptimizeCardDetailInfo);
            }else{
                mViewPublishDetailInfo = new TQViewPublishDetailInfo(context, null);
                mViewPublishDetailInfo.setOnClickListener(mOnClickListener);
                content.addView(mViewPublishDetailInfo);
            }

            typeView = v.findViewById(R.id.type);
            subjectView = v.findViewById(R.id.xueke);
            userHeaderView = v.findViewById(R.id.user_icon);
            userName = v.findViewById(R.id.user_name);
            publishStatus = v.findViewById(R.id.publishStatus);
            publishStatus.setVisibility(View.GONE);
        }

        public void bindData(TQPublishItem item){
            mPublishItem = item;
            if(mPublishItem == null) return;

            TQNewPublish lPublish = mPublishItem.getNewPublish();
            if(lPublish == null) return;

            if(lPublish.isPublish()){
                if(lPublish.getCreator().getId() == TQLogin.getCurrent().getUserInfo().getId()){
                    publishStatus.setImageResource(R.drawable.icon_wfbd);
                    publishStatus.setVisibility(View.VISIBLE);
                }else {
                    publishStatus.setVisibility(View.GONE);
                }
            }else{
                publishStatus.setImageResource(R.drawable.icon_wfb);
                publishStatus.setVisibility(View.VISIBLE);
            }


            if(subjectView != null){
                subjectView.setText(TQSystemDefine.subjectTitle(lPublish.getSubject()));
            }

            if(lPublish.getCreator() != null){
                RequestOptions lOptions = new RequestOptions().placeholder(R.drawable.tx2)
                        .circleCrop();
                String url = TQPictureUpload.downloadPath(lPublish.getCreator().getPic());
                Glide.with(context).load(url).apply(lOptions).into(userHeaderView);

                userName.setText(lPublish.getCreator().getName());
            }

            if(type == 0 && mViewPublishDetailInfo != null){
                mViewPublishDetailInfo.bindData(lPublish);
            }
            if(type == 1 && mTQViewOptimizeCardDetailInfo != null){
                mTQViewOptimizeCardDetailInfo.setBaseInfo(lPublish);
            }

            typeView.setImageResource(TQPublishType.typeIcon(item.getPublishType()));
        }

        private TQViewOptimizeCardDetailInfo.IClickListener mIClickListener = new TQViewOptimizeCardDetailInfo.IClickListener() {
            @Override
            public void dataStatices() {
                if(mClickListener != null){
                    mClickListener.dataStatices(mPublishItem);
                }
            }

            @Override
            public void arragQuestion() {
                if(mClickListener != null){
                    mClickListener.arragQuestion(mPublishItem);
                }
            }

            @Override
            public void arrageLeaveMsg() {
                if(mClickListener != null){
                    mClickListener.arrageLeaveMsg(mPublishItem);
                }
            }

            @Override
            public void arrageContent() {
                if(mClickListener != null){
                    mClickListener.arrageContent(mPublishItem);
                }
            }

            @Override
            public void showDetail() {
                if(mClickListener != null){
                    mClickListener.showDetail(mPublishItem);
                }
            }
        };


        private TQViewPublishDetailInfo.IOnClickListener mOnClickListener = new TQViewPublishDetailInfo.IOnClickListener() {
            @Override
            public void showViewer() {
                if(mClickListener != null){
                    mClickListener.showViewer(mPublishItem);
                }
            }

            @Override
            public void showLeverMsg() {
                if(mClickListener != null){
                    mClickListener.showLeverMsg(mPublishItem);
                }
            }

            @Override
            public void showDetail() {
                if(mClickListener != null){
                    mClickListener.showDetail(mPublishItem);
                }
            }
        };
    }

    public class TQHeaderViewHold extends TQItemViewHold{
        public TQHeaderViewHold(View v){
            super(v);
        }
    }

    public class TQFootViewHold extends RecyclerView.ViewHolder{
        private TQPublishSection mPublishItem;
        public TQFootViewHold(View v){
            super(v);

            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(mClickListener != null){
                        if(mPublishItem != null){
                            TQPublishItem lItem = (TQPublishItem)mPublishItem.getLastItem();

                            mClickListener.newLinker(mPublishItem);
                        }
                    }
                }
            });
        }

        public void bindSection(TQPublishSection pPublishItem){
            mPublishItem = pPublishItem;
        }
    }

    public interface IClickListener{
        void arrageLeaveMsg(TQPublishItem pItem);
        void arragQuestion(TQPublishItem pItem);

        void dataStatices(TQPublishItem pItem);
        void arrageContent(TQPublishItem pItem);

        void newLinker(TQPublishSection pItem);

        void showViewer(TQPublishItem pItem);
        void showLeverMsg(TQPublishItem pItem);
        void showDetail(TQPublishItem pItem);
    }
    private IClickListener mClickListener;
    public void setClickListener(IClickListener pListener){
        mClickListener = pListener;
    }
}
